<?php

include_once $REP_OUTILS . 'Selector.php';
include_once $REP_OUTILS . 'MyException.php';
// Pour FirePHP
include_once($REP_LIB . 'FirePHPCore/FirePHP.class.php');


/**
 * PariGest - class.Parametrage.php
 *
 * $Id$
 *
 * This file is part of PariGest.
 *
 * Automatically generated on 06.08.2011, 21:02:14 with ArgoUML PHP module 
 * (last revised $Date: 2010-01-12 20:14:42 +0100 (Tue, 12 Jan 2010) $)
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * include Compte
 *
 * @author firstname and lastname of author, <author@example.org>
 */
require_once('class.Compte.php');

/* user defined includes */
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000867-includes begin
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000867-includes end

/* user defined constants */
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000867-constants begin
// section 127-0-0-1-5eda895a:130ae729f01:-8000:0000000000000867-constants end

/**
 * Short description of class Parametrage
 *
 * @access public
 * @author firstname and lastname of author, <author@example.org>
 */
class Parametrage
{
    // --- ASSOCIATIONS ---
    // generateAssociationEnd : 

    // --- ATTRIBUTES ---

    /**
     * Short description of attribute IdParametrage
     *
     * @access private
     * @var Integer
     */
    private $IdParametrage = null;

    /**
     * Short description of attribute IdCompte
     *
     * @access private
     * @var Integer
     */
    private $IdCompte = null;

    /**
     * Short description of attribute MiseMini
     *
     * @access private
     * @var Integer
     */
    private $MiseMini = null;

    /**
     * Short description of attribute BeneficeMini
     *
     * @access private
     * @var Integer
     */
    private $BeneficeMini = null;

    /**
     * Short description of attribute Commentaire
     *
     * @access private
     * @var String
     */
    private $Commentaire = null;
    /**
    * Short description of attribute Commentaire
    *
    * @access private
    * @var String
    */
    private $ModeCumule = null;
    // --- OPERATIONS ---



    function __construct($db)
    {
      // Création de la connexion
      $this->maConnexion = $db;
      // Initialisation des requêtes
      $this->initRequete();
      // Préparation des requêtes
      $this->prepare();
      $this->trace = FirePHP::getInstance(true);
    }
    
    /**
     * Initialisation des requêtes préparées
     */
    private function initRequete(){
    
      $this->sqlInsert = "INSERT INTO Parametrage (IdCompte, MiseMini, BeneficeMini, Commentaire,ModeCumule) VALUES(?, ?, ?, ?, ?)";
      $this->sqlDelete = "DELETE FROM Parametrage WHERE `IdParametrage` = ? and `IdCompte`=?";
      $this->sqlUpdate = "UPDATE Parametrage SET MiseMini = ?, BeneficeMini = ?, Commentaire = ?, ModeCumule = ? WHERE `IdParametrage` = ? and `IdCompte`=?";
    
    }
    /**
     * Préparation des requêtes
     */
    private function prepare()
    {
      $this->reqInsert       = $this->maConnexion->prepare($this->sqlInsert);
      $this->reqDelete       = $this->maConnexion->prepare($this->sqlDelete);
      $this->reqUpdate       = $this->maConnexion->prepare($this->sqlUpdate);
    
    }
    
    /**
     * Short description of method affichage
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function affichage()
    {
      $this->trace->fb('Parametrage->affichage() : ' . $this->toStringClePrimaire());
      $user = new Parametrage($this->maConnexion);
      $user->setIdParametrage($this->getIdParametrage());
      $user->setIdCompte($this->getIdCompte());
      // Lecture de la première ligne
      $stmt = $user->select();
      $result = $stmt->fetch();

      $this->IdParametrage = $result['IdParametrage'];
      $this->IdCompte      = $result['IdCompte'];
      $this->MiseMini      = $result['MiseMini'];
      $this->BeneficeMini  = $result['BeneficeMini'];
      $this->Commentaire   = $result['Commentaire'];
      $this->ModeCumule    = $result['ModeCumule'];

      // Fermeture du curseur
      $stmt->closeCursor();
    }
    
    public function toStringClePrimaire()
    {
      return '|' . $this->IdParametrage . '|'. $this->IdCompte . '|';
    }

    /**
     * Short description of method insertion
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function insertion()
    {
      $this->trace->fb('insertion : ' . $this->toStringClePrimaire());
      // Requete d'insertion
      $champs = array($this->IdCompte, $this->MiseMini, $this->BeneficeMini, $this->Commentaire, $this->ModeCumule);

      try
      {
         $this->reqInsert->execute($champs);
//         return $this->maConnexion->lastInsertId();
      }
      catch(Exception $e)
      {
         //echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new MyException('Une erreur est survenue lors de l\'insertion du parametrage.', $this->reqInsert->errorInfo());
      }
    }

    /**
     * Short description of method modification
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function modification()
    {
      $this->trace->fb('modification : ' . $this->toStringClePrimaire());
      // Requete de modification
      $champs = array($this->MiseMini, $this->BeneficeMini, $this->Commentaire, $this->ModeCumule, $this->IdParametrage, $this->IdCompte);
      
      try
      {
        return $this->reqUpdate->execute($champs);
      }
      catch(Exception $e)
      {
        //echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
        throw new MyException('Une erreur est survenue lors de la modification du parametrage.', $this->reqUpdate->errorInfo());
      }     
    }

    /**
     * Short description of method suppression
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function suppression()
    {
      $this->trace->fb('suppression : ' . $this->toStringClePrimaire());
      // Requete de suppression
      try
      {
          $this->reqDelete->execute(array($this->IdParametrage, $this->IdCompte));
          $this->trace->fb('suppression réussie');          
      }
      catch(Exception $e)
      {
         //echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         //throw new Exception('Une erreur est survenue lors de la suppression dans classe. Cause : ' . $e->getMessage() );
         throw new MyException('Une erreur est survenue lors de la suppression du parametrage. ', $this->reqDelete->errorInfo());
         
      }
    }

    /**
     * Short description of method select
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function select()
    {
      $this->trace->fb('Parametrage.select');
      $select = new Selector();
      $champs = array("IdParametrage"=>$this->IdParametrage, "IdCompte"=>$this->IdCompte, "MiseMini"=>$this->MiseMini, "BeneficeMini"=>$this->BeneficeMini, "Commentaire"=>$this->Commentaire, "ModeCumule"=>$this->ModeCumule);
      
      foreach($champs as $cle=>$valeur) 
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Critères
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }
      
      // La Table
      $select->addFrom('Parametrage');
      $this->trace->fb('Requete : ' . $select->toString());
      //echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }

    /**
     * Short description of method selectMiseConseillee
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  UnlimitedInteger iCoteP
     * @param  Integer iIdParametrageP
     * @param  Integer iIdCompteP
     * @return mixed
     */
    /*public function selectMiseConseillee( $iCoteP)
    {
      if( $iCoteP == "" ) return "";
      $compte = new Compte($this->maConnexion);
      $compte->setIdCompte($this->IdCompte);
      $compte->affichage();
      $soldeVirtuel = $compte->selectSoldeVirtuel();
      
      $stmt = $this->select();
      $result = $stmt->fetch();
      
      $miseConseillee = (-$soldeVirtuel + $result['BeneficeMini']) / ($iCoteP - 1 );

      if( $miseConseillee < $result['MiseMini'] )
      {
        $miseConseillee = $result['BeneficeMini'] / ($iCoteP - 1 );
        //$miseConseillee = ceil($result['BeneficeMini'] / ($iCoteP - 1 ) * 10 ) / 10;
//         $miseConseillee = $result['MiseMini'];
      }
      
      $miseConseillee = ceil($miseConseillee * 10 ) / 10;
      
      return $miseConseillee;
    }*/
    
    /**
    * Short description of method getNextId
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return Integer
    */
    /*public function getNextId()
    {
      $select = new Selector();
    
      // Les Champs
      $select->addChamp('IFNULL(MAX(IdParametrage), 0)+1');
    
      // La Table
      $select->addFrom('Parametrage');
    
      // Les Critères
      //$select->addCritere('IdCompte', $this->IdCompte);
    
      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      $stmt = $this->maConnexion->query($select->toString());
      $row = $stmt->fetch();
      //$this->trace->fb('Résultat : ' .  $row[0]);
      return $row[0];
    }*/
    
    /**
    * Short description of method setIdParametrage
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    public function setIdParametrage( $iParametrageP)
    {
      $this->IdParametrage = $iParametrageP;
    }
    
    /**
     * Short description of method getIdParametrage
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIdParametrage()
    {
      return $this->IdParametrage;
    }
    
    /**
     * Short description of method setIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setIdCompte( $IdCompteP)
    {
      $this->IdCompte = $IdCompteP;
    }
    
    /**
     * Short description of method getIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIdCompte()
    {
      return $this->IdCompte;
    }
    
    /**
     * Short description of method setMiseMini
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setMiseMini( $iMiseMiniP)
    {
      $this->MiseMini = $iMiseMiniP;
    }
    
    /**
     * Short description of method getMiseMini
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getMiseMini()
    {
      return $this->MiseMini;
    }
    
    /**
     * Short description of method setBeneficeMini
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setBeneficeMini( $iBeneficeMiniP)
    {
      $this->BeneficeMini = $iBeneficeMiniP;
    }
    
    /**
     * Short description of method getBeneficeMini
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getBeneficeMini()
    {
      return $this->BeneficeMini;
    }
    
    /**
     * Short description of method setCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setCommentaire( $sCommentaireP)
    {
      $this->Commentaire = $sCommentaireP;
    }
    
    /**
     * Short description of method getCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getCommentaire()
    {
      return $this->Commentaire;
    }
    /**
    * Short description of method setmodeCumule
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    public function setModeCumule( $iModeCumuleP)
    {
      $this->ModeCumule = $iModeCumuleP;
    }
    
    /**
     * Short description of method getModeCumule
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getModeCumule()
    {
      return $this->ModeCumule;
    }

} /* end of class Parametrage */

?>